﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.IO;

namespace ControlExplorer
{
    static class PlatformUtils
    {
        #region constants
        public const string SL4OptionalPlatformSuffix = ".4";
        public const string SL5PlatformSuffix = ".5";
        public const string SL5XamlResDictSuffix = "_SL5rd";
        public const string SL5XamlPageSuffix = "_SL5";
        #endregion

        public static string AdjustPlatformName(string name, bool hasExtension)
        {
#if SL5
            if (string.IsNullOrWhiteSpace(name))
                return name;
            string ret = hasExtension ? GetNormalizedDirName(name) + Path.GetFileNameWithoutExtension(name) : name;
            if (ret.EndsWith(SL4OptionalPlatformSuffix, StringComparison.OrdinalIgnoreCase))
                ret = ret.Remove(ret.Length - SL4OptionalPlatformSuffix.Length);
            ret += SL5PlatformSuffix;
            if (hasExtension)
                ret += Path.GetExtension(name);
            return ret;
#else
            return name;
#endif
        }

        public static string StripPlatformSuffix(string name, bool hasExtension)
        {
#if SL5
            if (string.IsNullOrWhiteSpace(name))
                return name;
            string ret = hasExtension ? GetNormalizedDirName(name) + Path.GetFileNameWithoutExtension(name) : name;
            if (ret.EndsWith(SL5PlatformSuffix, StringComparison.OrdinalIgnoreCase))
                ret = ret.Remove(ret.Length - SL5PlatformSuffix.Length);
            if (hasExtension)
                ret += Path.GetExtension(name);
            return ret;
#else
            return name;
#endif
        }

        public static string AdjustPlatformXamlName(string name, bool isResourceDictionary)
        {
#if SL5
            if (string.IsNullOrWhiteSpace(name))
                return name;
            return GetNormalizedDirName(name) + Path.GetFileNameWithoutExtension(name) + 
                (isResourceDictionary ? SL5XamlResDictSuffix : SL5XamlPageSuffix) +
                Path.GetExtension(name);
#else
            return name;
#endif
        }

        private static string NormalizeDirName(string dirName)
        {
            if (string.IsNullOrWhiteSpace(dirName))
                return "";
            if (!(dirName.EndsWith("\\") || dirName.EndsWith("/")))
                dirName += "/";
            return dirName;
        }

        private static string GetNormalizedDirName(string path)
        {
            return NormalizeDirName(Path.GetDirectoryName(path));
        }

    }
}
